﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Store
{
    public partial class 材料收发存统计 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button_Sheet_Arrival_Click(object sender, EventArgs e)
        {
            this.Button_Sheet_Arrival.CssClass = "topsheet";
            this.Button_Sheet_In.CssClass = "normalsheet";
            this.Button_Sheet_Out.CssClass = "normalsheet";
            this.Button_Sheet_Store.CssClass = "normalsheet";

            this.Panel2.Visible = true;
            this.Panel3.Visible = false;
            this.Panel4.Visible = false;
            this.Label_Result1.Text = "请点击“统计”按钮进行统计";
        }

        protected void Button_Sheet_In_Click(object sender, EventArgs e)
        {
            this.Button_Sheet_Arrival.CssClass = "normalsheet";
            this.Button_Sheet_In.CssClass = "topsheet";
            this.Button_Sheet_Out.CssClass = "normalsheet";
            this.Button_Sheet_Store.CssClass = "normalsheet";

            this.Panel2.Visible = true;
            this.Panel3.Visible = false;
            this.Panel4.Visible = false;
            this.Label_Result1.Text = "请点击“统计”按钮进行统计";
        }

        protected void Button_Sheet_Out_Click(object sender, EventArgs e)
        {
            this.Button_Sheet_Arrival.CssClass = "normalsheet";
            this.Button_Sheet_In.CssClass = "normalsheet";
            this.Button_Sheet_Out.CssClass = "topsheet";
            this.Button_Sheet_Store.CssClass = "normalsheet";

            this.Panel3.Visible = true;
            this.Panel2.Visible = false;
            this.Panel4.Visible = false;
        }

        protected void Button_Sheet_Store_Click(object sender, EventArgs e)
        {
            this.Button_Sheet_Arrival.CssClass = "normalsheet";
            this.Button_Sheet_In.CssClass = "normalsheet";
            this.Button_Sheet_Out.CssClass = "normalsheet";
            this.Button_Sheet_Store.CssClass = "topsheet";

            this.Panel4.Visible = true;
            this.Panel3.Visible = false;
            this.Panel2.Visible = false;
        }

        protected void Button_Stat1_Click(object sender, EventArgs e)
        {

            string sql;
            bool daohuo=false;
            if (this.Button_Sheet_Arrival.CssClass == "topsheet")
            {
                daohuo=true;
            }
            if (daohuo)
            {
                sql ="select sum(实收),sum(实收*单价) from 材料到货记录  where 材料ID="+this.CaiLiao.Text;
            }
            else
            {
                sql = "select sum(实收),sum(实收*单价) from 材料入出库台账 where 使用单位ID=0 and 材料ID=" + this.CaiLiao.Text;
            }

            if (this.GongHuo.Text != "0")
            {
                if(daohuo)
                {
                    sql += " and 供货ID=" + this.GongHuo.Text;
                }
                else
                {
                    sql += " and 供货单位ID=" + this.GongHuo.Text;
                }
            }

            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                sql += " and 日期>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                sql += " and 日期<='" + Dt.ToShortDateString() + "'";
            }

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText =sql;
            Cmd.CommandType = CommandType.Text;
            SqlDataReader Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                if(Reader.IsDBNull(0))
                {
                    this.Label_Result1.Text = "查无记录";
                }
                else if (daohuo)
                {
                    this.Label_Result1.Text = "到货：" + ((double)Reader[0]).ToString() + " 价值：" + ((double)Reader[1]).ToString();
                }
                else
                {
                    this.Label_Result1.Text = "入库：" + ((double)Reader[0]).ToString() + " 价值：" + ((double)Reader[1]).ToString();
                }

            }
            else
            {
                this.Label_Result1.Text = "查无记录";
            }
            Reader.Close();
            Conn.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Dispose();
        }

    }
}
